package com.demo.app.service.entpr;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.demo.app.mapper.department.DepartmentMapper;
import com.demo.app.mapper.entpr.EntprMapper;
import enums.ErrorCodeEnum;
import model.dto.del.BatchDeleteDto;
import model.dto.entpr.InsertEntprDto;
import model.dto.entpr.UpdateEntprDto;
import model.entity.department.SysDepartment;
import model.entity.entpr.SysEntpr;
import model.vo.entpr.EntprDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.Result;
import util.SearchFilter;

import java.util.List;
import java.util.Objects;

/**
 * @program: fire_control
 * @description:
 * @author: fbl
 * @create: 2021-04-01 08:59
 **/
@Service
public class EntprService {
    @Autowired
    EntprMapper entprMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    public Result entprList(SearchFilter searchFilter) {
        PageHelper.startPage(searchFilter.getPageNum(), searchFilter.getPageSize());
        QueryWrapper<SysEntpr> sysEntprQueryWrapper = new QueryWrapper<SysEntpr>().eq("is_delete", 0);
        if (Objects.nonNull(searchFilter.getFilters())) {
            String entprName = searchFilter.getFilters().getString("entprName");
            sysEntprQueryWrapper.like("entpr_name", entprName);
        }
        List<SysEntpr> sysEntprs = entprMapper.selectList(sysEntprQueryWrapper);
        PageInfo<SysEntpr> sysEntprPageInfo = new PageInfo<>(sysEntprs);
        return Result.success(sysEntprPageInfo);
    }

    public Result entprDetail(Integer id) {
        QueryWrapper<SysEntpr> sysEntprQueryWrapper = new QueryWrapper<SysEntpr>().eq("is_delete", 0).eq("id", id);
        EntprDetailVo entprDetailVo = new EntprDetailVo();
        SysEntpr sysEntpr = entprMapper.selectOne(sysEntprQueryWrapper);
        BeanUtils.copyProperties(sysEntpr, entprDetailVo);
        // 查询企业下都有什么部门
        List<SysDepartment> departments = departmentMapper.selectList(new QueryWrapper<SysDepartment>().eq("entpr_id", id));
        entprDetailVo.setDepartments(departments);
        return Result.success(entprDetailVo);
    }

    public Result updateEntpr(UpdateEntprDto updateEntprDto) {
        if (!isUniqueEntprName(updateEntprDto.getEntprName())) {
            return Result.failure(ErrorCodeEnum.SYS_ERR_ENTPR_REPETITION);
        }
        // 修改企业基本信息
        SysEntpr sysEntpr = new SysEntpr();
        BeanUtils.copyProperties(updateEntprDto, sysEntpr);
        entprMapper.updateById(sysEntpr);
        return Result.success();
    }

    public Result delEntpr(BatchDeleteDto batchDeleteDto){
        QueryWrapper<SysEntpr> queryWrapper = new QueryWrapper<SysEntpr>().in("id", batchDeleteDto.getIds());
        SysEntpr sysEntpr = new SysEntpr();
        sysEntpr.setDelFlag(true);
        entprMapper.update(sysEntpr,queryWrapper);
        return Result.success();
    }

    public Result addEntpr(InsertEntprDto insertEntprDto){
        if (!isUniqueEntprName(insertEntprDto.getEntprName())) {
            return Result.failure(ErrorCodeEnum.SYS_ERR_ENTPR_REPETITION);
        }
        SysEntpr sysEntpr = new SysEntpr();
        BeanUtils.copyProperties(insertEntprDto,sysEntpr);
        entprMapper.insert(sysEntpr);
        return Result.success();
    }
    private boolean isUniqueEntprName(String name) {
        QueryWrapper<SysEntpr> sysEntprQueryWrapper = new QueryWrapper<SysEntpr>().eq("is_delete", 0).eq("entpr_name", name);
        List<SysEntpr> sysEntprs = entprMapper.selectList(sysEntprQueryWrapper);
        return CollectionUtil.isEmpty(sysEntprs);
    }
}
